package com.custom.base.manager

import android.app.Activity
import android.content.Context
import android.util.Log
import java.util.Stack

/**
 * 作者：
 * 时间：12/10/20
 * 描述：activity 管理
 **/
class SDActivityManager private constructor() {
    /**
     * 获取最后一个activity
     */
    val lastActivity: Activity
        get() {
            var activity: Activity? = null
            try {
                activity = mStackActivity.lastElement()
            } catch (e: Exception) {
            }

            return activity!!
        }

    val isEmpty: Boolean
        get() = mStackActivity.isEmpty()

//    fun init() {
//        Log.e("~~~", "mStackActivity:init " )
//        mStackActivity = Stack()
//    }

    fun onCreate(activity: Activity) {
        addActivity(activity)
    }

    fun onResume(activity: Activity) {
        addActivity(activity)
    }

    fun onDestroy(activity: Activity) {
        removeActivity(activity)
    }

    /**
     * 添加activity
     */
    private fun addActivity(activity: Activity) {
        if (!mStackActivity.contains(activity)) {
            mStackActivity.add(activity)
        }
    }

    /**
     * 删除activity
     */
    private fun removeActivity(activity: Activity?) {
        if (activity != null) {
            mStackActivity.remove(activity)
        }
    }

    /**
     * 判断是否是最后一个
     */
    fun isLastActivity(activity: Activity?): Boolean {
        return if (activity != null) {
            lastActivity === activity
        } else {
            false
        }
    }
    /**
     * 判断是否是最后一个
     */
    fun isLastActivity(cls: Class<*>): Boolean {
        return if (cls != null) {
            lastActivity.javaClass === cls
        } else {
            false
        }
    }
    /**
     * 判断activity是否存在
     * @param cls
     * @return
     */
    fun isExistActivity(cls: Class<*>): Boolean {
        val it = mStackActivity.iterator()
        while (it.hasNext()) {
            val act = it.next()
            if (act.javaClass == cls) {
                return true
            }
        }
        return false
    }

    /**
     * 结束最后的Activity
     */
    fun finishLastActivity() {
        lastActivity?.finish()
    }
    /**
     * 结束指定类名的Activity
     */
    fun finishActivity(cls: Class<*>) {
        val it = mStackActivity.iterator()
        while (it.hasNext()) {
            val act = it.next()
            if (act.javaClass == cls) {
                it.remove()
                act.finish()
            }
        }
    }

    /**
     * 结束除了制定的activity之外所有的activity
     */
    fun finishAllClassActivityExcept(activity: Activity) {
        val it = mStackActivity.iterator()
        while (it.hasNext()) {
            val act = it.next()
            if (act.javaClass == activity.javaClass && act !== activity) {
                it.remove()
                act.finish()
            }
        }
    }

    /**
     * 结束所有activity
     */
    fun finishAllActivity() {
        val it = mStackActivity.iterator()
        while (it.hasNext()) {
            val act = it.next()
            it.remove()
            act.finish()
        }
    }
    /**
     * 结束除了指定的累名之外所有的activity
     */
    fun finishAllActivityExcept(cls: Class<*>) {
        val it = mStackActivity.iterator()
        while (it.hasNext()) {
            val act = it.next()
            if (act.javaClass != cls) {
                it.remove()
                act.finish()
            }
        }
    }
    /**
     * 结束除了制定的activity之外所有的activity
     */
    fun finishAllActivityExcept(activity: Activity) {
        val it = mStackActivity.iterator()
        while (it.hasNext()) {
            val act = it.next()
            if (act !== activity) {
                it.remove()
                act.finish()
            }
        }
    }

    companion object {
        private var mStackActivity: Stack<Activity> = Stack()
        private var mInstance: SDActivityManager?=null

        val instance: SDActivityManager
            get() {
                if (mInstance == null) {
                    synchronized(SDActivityManager::class.java) {
                        if (mInstance == null) {
                            mInstance = SDActivityManager()
                        }
                    }
                }
                return mInstance!!
            }
    }
}